package fr.SDroid.Util;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

public class LigneAdapter extends SimpleExpandableListAdapter{

	private List<? extends List<? extends Map<String, ?>>> childData;
	private String[] childFrom;
	private int[] childTo;
	
	public LigneAdapter(Context context,
			List<? extends Map<String, ?>> groupData, int groupLayout,
			String[] groupFrom, int[] groupTo,
			List<? extends List<? extends Map<String, ?>>> childData,
			int childLayout, String[] childFrom, int[] childTo) {
		
		
		super(context, groupData, groupLayout, groupFrom, groupTo, childData,
				childLayout, childFrom, childTo);

		this.childData = childData;
		this.childFrom = childFrom;
		this.childTo = childTo;
	} 

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View v;
		if (convertView == null) {
			v = newChildView(isLastChild, parent);
		} else {
			v = convertView;
		}
		bindView(v, childData.get(groupPosition).get(childPosition), childFrom, childTo);
		return v;
	}
	
	 private void bindView(View view, Map<String, ?> data, String[] from, int[] to) {
		int len = to.length;
		
		for (int i = 0; i < len; i++) {
			if(from[i].equals("image")) {
				ImageView v = (ImageView)view.findViewById(to[i]);
				if(v != null) {
					v.setImageDrawable((Drawable)data.get(from[i]));
				}
			} else {
				TextView v = (TextView)view.findViewById(to[i]);
				if (v != null) {
					v.setText((String)data.get(from[i]));
				}
			}
		}
	}
}
